//
//  LTLhomeModel.m
//  WeiBo
//
//  Created by LiTaiLiang on 16/9/30.
//  Copyright © 2016年 LTL. All rights reserved.
//

#import "LTLhomeModel.h"
#import "LTLPhone.h"

@implementation LTLhomeModel

//-(instancetype)initLTLhomeModelDict:(NSDictionary *)dict
//{
//    if (self = [super init]) {
//        
//        //[self.text setValue:dict forKey:@"text"];
//        
//        self.text =  dict[@"text"];
//        self.source= dict[@"source"];
//        self.created_at= dict[@"created_at"];
//        self.favorited= dict[@"favorited"];
//        self.idstr= dict[@"idstr"];
//        self.pic_urls= dict[@"pic_urls"];
//        self.reposts_count = (int)dict[@"reposts_count"];
//        self.comments_count = (int)dict[@"comments_count"];
//        self.attitudes_count = (int)dict[@"attitudes_count"];
//        self.user = [LTLUser LTLUserDict:dict[@"user"]];
//        
//    }
//    return self;
//}
//+(instancetype)LTLhomeModelDict:(NSDictionary *)dict
//{
//    return [[LTLhomeModel alloc]initLTLhomeModelDict:dict];
//}
+(NSDictionary *)mj_objectClassInArray
{
    return @{@"pic_urls" : [LTLPhone class] };
}
///重写source赋值
-(void)setSource:(NSString *)source
{
//    if (source != nil) {
//        //截取字符串
//        //开始位置
//        NSRange startRange = [source rangeOfString:@">"];
//        //结束位置
//        NSRange endRange = [source rangeOfString:@"<"];
//        //截取的位置及长度
//        NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
//        //截取
//        NSString *result = [source substringWithRange:range];
//        
//        _source = result;
//    } else {
//        _source = source;
//    }
//    NSLog(@"%@",source);
    if (source != nil) {
        // 组装一个字符串，需要把里面的内容解析出来
//        NSString *urlString = source;
        // NSRegularExpression类里面调用表达的方法需要传递一个NSError的参数。下面定义一个
        NSError *error;
        // http+:[^\\s]* 这个表达式是检测一个网址的。(?<=title\>).*(?=</title)截取html文章中的<title></title>中内文字的正则表达式
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: @"(?<=>).*(?=<)" options: 0 error:&error];
        
        NSTextCheckingResult *firstMatch=[regex firstMatchInString:source options: 0 range:NSMakeRange( 0 , [source length])];
        
        NSRange resultRange = [firstMatch rangeAtIndex: 0 ];
        // 从urlString当中截取数据
        NSString *result=[NSString stringWithFormat:@"来自 %@",[source substringWithRange:resultRange]];
        
        _source = result;

    } else {
        _source = source;
    }
}
//微博发布时间
-(NSString *)created_at
{
    return [self dateTime:_created_at];
}
-(NSString *)dateTime:(NSString *)time
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    //真机需要设置时区
    formatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    formatter.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
    NSDate *created_at = [formatter dateFromString:time];
    //当前时间
    NSDate *selfDate = [NSDate date];
    //创建日历类
    NSCalendar *Calendar = [NSCalendar currentCalendar];
    //取出时间
    NSCalendarUnit timeUnit = NSCalendarUnitYear | NSCalendarUnitDay |NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond  ;
    
    NSDateComponents *timeDate = [Calendar components:timeUnit fromDate:created_at];
    NSDateComponents *currentTimeDate = [Calendar components:timeUnit fromDate:selfDate];
    //今年
    if (timeDate.year == currentTimeDate.year)
    {
        //今天
        if (timeDate.day == currentTimeDate.day) {
            //时间差
            NSDateComponents *cha = [Calendar components:timeUnit fromDate:created_at toDate:selfDate options:NSCalendarWrapComponents];
        
            //一个小时之内
            if (cha.hour<1) {
                
                if (cha.minute<1) {
                    return @"刚刚";
                } else {
                    return [NSString stringWithFormat:@"%ld分钟前",(long)cha.minute];
                }
            } else {
               return [NSString stringWithFormat:@"%ld个小时前",(long)cha.hour];
            }
        }
        //昨天
        else if (timeDate.day == currentTimeDate.day-1)
        {
            formatter.dateFormat = @"昨天 HH:mm";
            return [formatter stringFromDate:created_at];
        }
        //其他天
        else
        {
            formatter.dateFormat = @"MM-dd HH:mm";
            return [formatter stringFromDate:created_at];
        }
    }//非今年
    else
    {
        formatter.dateFormat = @"yyyy-MM-dd HH:mm";
        return [formatter stringFromDate:created_at];
    }
}
@end
